%% Functions to plot intervals

function [fig, sortOrder] = plot_intervals(hat_vartheta, bar_tau, predict_se)
    
    % Initialization
    N = length(bar_tau);

    % Calculate the recentered intervals
    re_lower_bounds = bar_tau - hat_vartheta - 1.282 * predict_se;
    re_upper_bounds = bar_tau - hat_vartheta + 1.282 * predict_se;
    
    % Sort intervals based on ascending lower_bounds, then by upper_bounds
    [~, sortOrder] = sortrows(re_lower_bounds);
    re_lower_bounds = re_lower_bounds(sortOrder);
    re_upper_bounds = re_upper_bounds(sortOrder);

    % Prepare the plot
    fig = figure;
    hold on;
    
    for i = 1:N
        % Plot regular bar with pink semi-transparency
        rectangle('Position', [i - 0.4, re_lower_bounds(i), 0.8, re_upper_bounds(i) - re_lower_bounds(i)], ...
                  'FaceColor', [1, 0.6, 0.6, 0.5], 'EdgeColor', 'none'); % Pink
    end

    xtick = 0:2:16;
    xticks(xtick);
    xlim([xtick(1), xtick(end)]);

    ylim([-325 325]);
    % ylim([-400 300]);
    yline(0, 'k--', 'LineWidth', 1);

    % Adjust font size of the x-axis labels
    set(gca, 'FontSize', 18);
   
    hold off;

end
